// pages/myOrders/myOrders.js
const util = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    nonPay_orders:[],
    complete_order:[],
    pageIndex_nonPay:1,
    pageIndex_comp:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  toDetail:(e)=>{
    wx.navigateTo({url:"../orderDetail/orderDetail?order="+JSON.stringify(e.currentTarget.dataset.order)})
  },
  toPay:(e)=>{
    console.log(e)
    let openId = wx.getStorageSync('openId'),
      orderId = e.currentTarget.dataset.target.orderId;
    console.log(openId);
    console.log(orderId);
    util.post({
      url: 'https://groomtea.icebartech.com/api/miniApp/order/orderPay',
      data: {
        openId: openId,
        orderId: orderId
      },
      success: res => {
        let result = JSON.parse(res.data.bussData);
        result['success'] = (res) => {
          console.log(result.id);
          wx.reLaunch({
            url: '../payRes/payRes?orderId=' + result.id,
          })
        }
        result['fail'] = (err) => {
          console.log(err);
        }
        wx.requestPayment(result);

      },
      fail: err => {
        console.log(err);
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getNonPayOrder();
    this.getCompleOrders();
  },

  getNonPayOrder(){
    var that = this;
    wx.showLoading({
      title: '正在加载',
    })
    let nonPay_orders = that.data.nonPay_orders,
      pageIndex_nonPay = that.data.pageIndex_nonPay
    util.post({
      url: "https://groomtea.icebartech.com/api/miniApp/order/orderPage",
      data: {
        "orderStatus": "non_payment",
        "pageIndex": pageIndex_nonPay,
        "pageSize": 5
      },
      success: (res) => {
        
        nonPay_orders = [...nonPay_orders,...res.data.bussData]
        that.setData({
          nonPay_orders,
          pageIndex_nonPay: pageIndex_nonPay+1
        })
      },
      complete:()=>{
        wx.hideLoading();
      }
    });
  },

  getCompleOrders(){
    var that = this;
    let complete_order = that.data.complete_order,
      pageIndex_comp = that.data.pageIndex_comp;
    util.post({
      url: "https://groomtea.icebartech.com/api/miniApp/order/orderPage",
      data: {
        "orderStatus": "finish",
        "pageIndex": pageIndex_comp,
        "pageSize": 5
      },
      success: (res) => {
        complete_order = [...complete_order, ...res.data.bussData]
        that.setData({
          complete_order,
          pageIndex_comp: pageIndex_comp+1
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    let data = this.data;
    if(data.active==0){
      that.getNonPayOrder();
    }
    if(data.active==1){
      that.getCompleOrders();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})